package it.producer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer_HelloWorld {

    public static void main(String[] args) throws IOException, TimeoutException {

        //1. 建立连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        //2. 设置参数
        factory.setHost("192.168.1.113"); //ip  默认值localhost
        factory.setPort(5672);//端口  默认值5672
        factory.setUsername("guest");//用户名
        factory.setPassword("guest");//密码

        //3. 创建连接 Connection
        Connection connection = factory.newConnection();

        //4. 创建管道 Channel
        Channel channel = connection.createChannel();

        //5. 创建队列 Queue
        /**
         * queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
         *          1. queue:队列名称
         *          2. durable:是否持久化。mq重启后还在
         *          3. exclusive:
         *              是否独占，只有一个消费者监听这个队列
         *              connection关闭后是否删除队列
         *              一般情况设置为false
         *          4. autoDelete:是否自动删除。当没有consumer时自动删除
         *          5. arguments:参数
         * */
        channel.queueDeclare("hello_world",true,false,false,null);

        //6. 发送消息
        /**
         * basicPublish(String exchange, String routingKey, boolean mandatory, BasicProperties props, byte[] body)
         *          1. exchange: 交换机名称 简单模式下为 ""
         *          2. routingKey: 路由器名称
         *          3. props:配置信息
         *          4. body:发送消息数据
         *
         * */
        String message = "hello rabbit.....";
        channel.basicPublish("","hello_world",null,message.getBytes());

        //7. 释放资源
        channel.close();
        connection.close();

    }

}
